import React, { useEffect, useMemo } from 'react';

import { WelcomeGrid, WelcomeLayout } from '@/pages/HomePage/components';

import { useWelcomeReducer, useWelcomeUserInfo } from '@/pages/HomePage/hooks';

import { Spin } from 'antd';

import '../../../node_modules/react-grid-layout/css/styles.css';
import '../../../node_modules/react-resizable/css/styles.css';

import './styles.less';
import { WelcomeBodyLayout } from '@/pages/HomePage/components/WelcomeBodyLayout';
import { HomePageApi } from './type';

import { getPosIDs, getTenantID, getUserID } from '@/utils';
import { AnnounceView } from './HomeViews/HomeAnnounce';
import {
  ContractHomeView,
  ContractWarnView,
  NeedHomeView,
  ApplyHomeView,
  DocumentHomeView,
  ClarifyHomeView,
  CalibrateHomeView,
  IntentHomeView,
  ChangeHomeView,
} from './HomeViews';

// const ResponsiveReactGridLayout = WidthProvider(Responsive);
const HomePage: React.FC<HomePageApi.IHomePage> = ({ curRes, baseLoginProps, homeDispatch }) => {
  const prefixCls = 'welcome-';
  const {
    // isResizable,
    // isDraggable,
    layoutInfo,
    commonDispatch,
  } = useWelcomeReducer();
  /**
   * 获取是否开启布局
   */
  const {
    openWelcomeLayout: { isDraggable, isResizable, monitorLayout, layoutLoading },
    welcomeLayout,
    userInfo,
  } = baseLoginProps;

  const { loading, reloadUserInfo, userObjInfo } = useWelcomeUserInfo(homeDispatch);

  /**
   * 获取最新用户数据
   */
  const getUserInfoMemo = useMemo(() => {
    let newUserInfo: Record<string, any> = userObjInfo && userObjInfo?.id ? userObjInfo : userInfo;
    return newUserInfo;
  }, [userObjInfo, userInfo]);

  useEffect(() => {
    if (monitorLayout) {
      reloadUserInfo();
    }
  }, [monitorLayout]);

  /**
   * 处理刷新 布局默认问题
   */
  const layoutInfoObj = useMemo(() => {
    let layoutObj: any = {};
    let newUserObj: Record<string, any> = userObjInfo && userObjInfo?.id ? userObjInfo : userInfo;
    const target = newUserObj?.muserLayoutList?.find((i: any) => {
      return i?.layoutType === `${getTenantID()}10011001` && i?.userPositionID == getPosIDs();
    });
    target?.layoutInfo?.lg?.forEach((item: any) => {
      layoutObj[`${item?.i}`] = item;
    });
    return layoutObj;
  }, [userInfo, layoutInfo, curRes, userObjInfo]);

  return (
    <WelcomeLayout
      value={{
        prefixCls,
        userInfo: getUserInfoMemo,
        isResizable,
        isDraggable,
        layoutInfo,
        welcomeLayout,
        commonDispatch,
        reloadUserInfo,
        homeDispatch,
      }}
    >
      <Spin spinning={layoutLoading ? true : false}>
        <WelcomeBodyLayout>
          {getUserInfoMemo && getUserInfoMemo?.id && getUserID() && curRes && (
            <WelcomeGrid>
              {curRes?.['/announceController/getIssueAnnounceList'] && (
                <div
                  key={'homeAnnounce'}
                  data-grid={
                    layoutInfoObj?.homeAnnounce
                      ? layoutInfoObj?.homeAnnounce
                      : // : { x: 0, y: 12, w: 6, h: 8 }
                        { x: 0, y: 0, w: 6, h: 8 }
                  }
                >
                  <AnnounceView curRes={curRes} />
                </div>
              )}

              {/* 合同首页 */}
              {(curRes?.['/contractController/draftHomePage'] ||
                curRes?.['/contractController/dealHomePage'] ||
                curRes?.['/contractController/dealedHomePage'] ||
                curRes?.['/contractController/returnHomePage']) && (
                <div
                  key={'homeContract'}
                  data-grid={
                    layoutInfoObj?.homeContract
                      ? layoutInfoObj?.homeContract
                      : // : { x: 0, y: 12, w: 6, h: 8 }
                        { x: 0, y: 0, w: 6, h: 8 }
                  }
                >
                  <ContractHomeView curRes={curRes} />
                </div>
              )}

              {/** 督办事项 */}
              {curRes?.['/warnController/contractHomePage'] && curRes?.['/warnController/dataGrid/{fieldName}'] && (
                <div key={'homeWarn'} data-grid={layoutInfoObj?.homeWarn ? layoutInfoObj?.homeWarn : { x: 8, y: 0, w: 6, h: 8 }}>
                  <ContractWarnView curRes={curRes} />
                </div>
              )}

              {/**采购需求 */}
              {(curRes?.['/purchaseNeedController/draftHomePage'] ||
                curRes?.['/purchaseNeedController/dealHomePage'] ||
                curRes?.['/purchaseNeedController/dealedHomePage'] ||
                curRes?.['/purchaseNeedController/returnHomePage']) && (
                <div key={'homePurchaseNeed'} data-grid={layoutInfoObj?.homePurchaseNeed ? layoutInfoObj?.homePurchaseNeed : { x: 0, y: 8, w: 6, h: 8 }}>
                  <NeedHomeView curRes={curRes} />
                </div>
              )}

              {/**采购变更 */}
              {(curRes?.['/purchaseSourceChangeController/draftHomePage'] ||
                curRes?.['/purchaseSourceChangeController/dealHomePage'] ||
                curRes?.['/purchaseSourceChangeController/dealedHomePage'] ||
                curRes?.['/purchaseSourceChangeController/returnHomePage']) && (
                <div
                  key={'purchaseSourceChange'}
                  data-grid={layoutInfoObj?.purchaseSourceChange ? layoutInfoObj?.purchaseSourceChange : { x: 0, y: 32, w: 6, h: 8 }}
                >
                  <ChangeHomeView curRes={curRes} />
                </div>
              )}

              {/**采购意向公开 */}
              {(curRes?.['/purchaseSourceIntentController/draftHomePage'] ||
                curRes?.['/purchaseSourceIntentController/dealHomePage'] ||
                curRes?.['/purchaseSourceIntentController/dealedHomePage'] ||
                curRes?.['/purchaseSourceIntentController/returnHomePage']) && (
                <div
                  key={'purchaseSourceIntent'}
                  data-grid={layoutInfoObj?.purchaseSourceIntent ? layoutInfoObj?.purchaseSourceIntent : { x: 8, y: 24, w: 6, h: 8 }}
                >
                  <IntentHomeView curRes={curRes} />
                </div>
              )}

              {/**采购申请 */}
              {(curRes?.['/purchaseApplyController/draftHomePage'] ||
                curRes?.['/purchaseApplyController/dealHomePage'] ||
                curRes?.['/purchaseApplyController/dealedHomePage'] ||
                curRes?.['/purchaseApplyController/returnHomePage']) && (
                <div key={'purchaseApply'} data-grid={layoutInfoObj?.purchaseApply ? layoutInfoObj?.purchaseApply : { x: 8, y: 8, w: 6, h: 8 }}>
                  <ApplyHomeView curRes={curRes} />
                </div>
              )}

              {/**采购文件编制 */}
              {(curRes?.['/purchaseSourceController/draftHomePage'] ||
                curRes?.['/purchaseSourceController/dealHomePage'] ||
                curRes?.['/purchaseSourceController/dealedHomePage'] ||
                curRes?.['/purchaseSourceController/returnHomePage']) && (
                <div key={'purchaseSource'} data-grid={layoutInfoObj?.purchaseSource ? layoutInfoObj?.purchaseSource : { x: 0, y: 16, w: 6, h: 8 }}>
                  <DocumentHomeView curRes={curRes} />
                </div>
              )}

              {/**采购招标澄清 */}
              {(curRes?.['/purchaseSourceClarifyController/draftHomePage'] ||
                curRes?.['/purchaseSourceClarifyController/dealHomePage'] ||
                curRes?.['/purchaseSourceClarifyController/dealedHomePage'] ||
                curRes?.['/purchaseSourceClarifyController/returnHomePage']) && (
                <div
                  key={'purchaseSourceClarify'}
                  data-grid={layoutInfoObj?.purchaseSourceClarify ? layoutInfoObj?.purchaseSourceClarify : { x: 8, y: 16, w: 6, h: 8 }}
                >
                  <ClarifyHomeView curRes={curRes} />
                </div>
              )}

              {/**采购定标 */}
              {(curRes?.['/purchaseSourceCalibrateController/draftHomePage'] ||
                curRes?.['/purchaseSourceCalibrateController/dealHomePage'] ||
                curRes?.['/purchaseSourceCalibrateController/dealedHomePage'] ||
                curRes?.['/purchaseSourceCalibrateController/returnHomePage']) && (
                <div
                  key={'purchaseSourceCalibrate'}
                  data-grid={layoutInfoObj?.purchaseSourceCalibrate ? layoutInfoObj?.purchaseSourceCalibrate : { x: 0, y: 24, w: 6, h: 8 }}
                >
                  <CalibrateHomeView curRes={curRes} />
                </div>
              )}
            </WelcomeGrid>
          )}
        </WelcomeBodyLayout>
      </Spin>
    </WelcomeLayout>
  );
};

export default HomePage;
